package com.sugar.real.estate.module.business.controller.admin.userfollow;

import com.sugar.real.estate.framework.apilog.core.annotation.ApiAccessLog;
import com.sugar.real.estate.framework.common.pojo.CommonResult;
import com.sugar.real.estate.framework.common.pojo.PageParam;
import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.common.util.object.BeanUtils;
import com.sugar.real.estate.framework.excel.core.util.ExcelUtils;
import com.sugar.real.estate.module.business.controller.admin.userfollow.vo.UserFollowPageReqVO;
import com.sugar.real.estate.module.business.controller.admin.userfollow.vo.UserFollowRespVO;
import com.sugar.real.estate.module.business.dal.dataobject.userfollow.UserFollowDO;
import com.sugar.real.estate.module.business.service.userfollow.UserFollowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.sugar.real.estate.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.sugar.real.estate.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 用户关注")
@RestController
@RequestMapping("/business/user-follow")
@Validated
public class UserFollowController {

    @Resource
    private UserFollowService userFollowService;

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户关注")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:user-follow:delete')")
    public CommonResult<Boolean> deleteUserFollow(@RequestParam("id") Long id) {
        userFollowService.deleteUserFollow(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户关注")
                @PreAuthorize("@ss.hasPermission('business:user-follow:delete')")
    public CommonResult<Boolean> deleteUserFollowList(@RequestParam("ids") List<Long> ids) {
        userFollowService.deleteUserFollowListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户关注")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:user-follow:query')")
    public CommonResult<UserFollowRespVO> getUserFollow(@RequestParam("id") Long id) {
        UserFollowDO userFollow = userFollowService.getUserFollow(id);
        return success(BeanUtils.toBean(userFollow, UserFollowRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户关注分页")
    @PreAuthorize("@ss.hasPermission('business:user-follow:query')")
    public CommonResult<PageResult<UserFollowRespVO>> getUserFollowPage(@Valid UserFollowPageReqVO pageReqVO) {
        return success(userFollowService.getUserFollowPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户关注 Excel")
    @PreAuthorize("@ss.hasPermission('business:user-follow:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserFollowExcel(@Valid UserFollowPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "用户关注.xls", "数据", UserFollowRespVO.class,
                userFollowService.getUserFollowPage(pageReqVO).getList());
    }

}